package br.gov.cemaden.library.model.alerta;

import java.util.EnumSet;
import java.util.NavigableMap;
import java.util.TreeMap;

/**
 *
 * @author alan
 */
public enum Direcao {
    
    N(0.0), S(90.0), E(180.0), W(360.0), 
    NE(45.0), SE(135.0), NW(225.0), SW(315.0), 
    NNE(22.5), ENE(67.5), ESE(112.5), SSE(157.5), SSW(202.5), WSW(247.5), WNW(292.5), NNW(337.5);
    
    private final Double angulo;
    
    private static final NavigableMap<Double, Direcao> lookup = new TreeMap<Double, Direcao>();
    
    static {
        for (Direcao direcao : EnumSet.allOf(Direcao.class)) {
            lookup.put(direcao.getAngulo(), direcao);
        }
    }
    
    Direcao(Double angulo){
        this.angulo = angulo;
    }

    public Double getAngulo() {
        return angulo;
    }
    
    public static Direcao get(Double angulo){
        
        if(lookup.containsKey(angulo))
            return lookup.get(angulo);
        
        Double before = lookup.floorKey(angulo);
        Double after = lookup.ceilingKey(angulo);
        
        if(before == null) 
            return lookup.get(after);
        if(after == null) 
            return lookup.get(before);
        
        return (angulo - before > after - angulo) ? lookup.get(after) : lookup.get(before);
    }    
    
}
